unit FMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, Mask, RXSpin, frxClass, DB, ADODB, RxMemDS,
  Grids, DBGrids, frxDBSet, DBClient, frxBarcode, StrUtils, inifiles,
  frxDesgn;

type
  TFormMain = class(TForm)
    lTitulo: TLabel;
    gbPosicion: TGroupBox;
    Horizonal: TLabel;
    eHorizontal: TRxSpinEdit;
    Label2: TLabel;
    eVertical: TRxSpinEdit;
    gbNumeracion: TGroupBox;
    Label3: TLabel;
    eNumIni: TEdit;
    Label4: TLabel;
    eNumFin: TEdit;
    BtnImprimir: TBitBtn;
    frxDBDatos: TfrxDBDataset;
    TablaCSV: TADOTable;
    frxReport: TfrxReport;
    Label6: TLabel;
    cbDocs_x_hoja: TComboBox;
    gbLog: TGroupBox;
    mLog: TMemo;
    frxDesigner: TfrxDesigner;
    SpeedButton1: TSpeedButton;
    procedure BtnImprimirClick(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure FormCreate(Sender: TObject);
    procedure cbDocs_x_hojaChange(Sender: TObject);
    procedure lTituloDblClick(Sender: TObject);
  private
    { Private declarations }
    Function GenerarDato( Numero, Ult_Num: Integer): String;
    Procedure GenerarNumeros_6;
    Procedure Log;
  public
    { Public declarations }
  end;

var
  FormMain: TFormMain;

implementation

{$R *.dfm}

procedure TFormMain.BtnImprimirClick(Sender: TObject);
var
  i   : Integer;
  obj : TComponent;
begin
  TablaCSV.Close;
  if cbDocs_x_hoja.ItemIndex = 0 then
  begin
    GenerarNumeros_6;
    frxReport.LoadFromFile( 'rep_CodBarras_Multas_6.fr3');

    for i:=0 to frxReport.ComponentCount-1 do
    begin
      obj:= frxreport.Components[i]; // find the page
      if obj.ClassName = 'TfrxMemoView' then
      begin
        TfrxMemoView( Obj).Top  := TfrxMemoView( Obj).Top  + eVertical.Value * 3.78;
        TfrxMemoView( Obj).Left := TfrxMemoView( Obj).Left + eHorizontal.Value * 3.78;
      end
      else if obj.ClassName = 'TfrxBarCodeView' then
      begin
        TfrxBarCodeView( Obj).Top  := TfrxBarCodeView( Obj).Top  + eVertical.Value * 3.78;
        TfrxBarCodeView( Obj).Left := TfrxBarCodeView( Obj).Left + eHorizontal.Value * 3.78;
      end;
    end;
    frxReport.PrepareReport();
    Log;
    frxReport.Print;
  end;
end;




procedure TFormMain.Log;
begin
  mLog.Lines.Insert( 0, FormatDateTime( 'dd/mm/yyyy hh:nn:ss', now) + ' --> ' + FormatFloat( '#,##0', StrToInt( eNumIni.Text)) + ' - ' + FormatFloat( '#,##0', StrToInt( eNumFin.Text)) + ' (' + cbDocs_x_hoja.Text + ')');
end;

procedure TFormMain.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
var
  Ini: TIniFile;
  Sec: String;
begin
  Sec := 'Config_' + cbDocs_x_hoja.Text;
  Ini := TIniFile.Create( ExtractFilePath( Application.ExeName) + changeFileExt( ExtractFileName( Application.ExeName),'.ini'));
  try
    Ini.WriteString( Sec, 'Desp_X' , eHorizontal.Text);
    Ini.WriteString( Sec, 'Desp_Y' , eVertical.Text);
    Ini.WriteString( Sec, 'Num_Ini', eNumIni.Text);
    Ini.WriteString( Sec, 'Num_Fin', eNumFin.Text);
    Ini.WriteInteger( 'Cfg', 'Tipo_Imp', cbDocs_x_hoja.ItemIndex);
  finally
    Ini.Free;
  end;
end;

procedure TFormMain.FormCreate(Sender: TObject);
var
  Ini: TIniFile;
begin
  Ini := TIniFile.Create( ExtractFilePath( Application.ExeName) + changeFileExt( ExtractFileName( Application.ExeName),'.ini'));
  try
    cbDocs_x_hoja.ItemIndex := Ini.ReadInteger( 'Cfg', 'Tipo_Imp', 0);
  finally
    Ini.Free;
  end;
  cbDocs_x_hojaChange( Sender);
end;

procedure TFormMain.cbDocs_x_hojaChange(Sender: TObject);
var
  Ini: TIniFile;
  Sec: String;
begin
  Sec := 'Config_' + cbDocs_x_hoja.Text;
  Ini := TIniFile.Create( ExtractFilePath( Application.ExeName) + changeFileExt( ExtractFileName( Application.ExeName),'.ini'));
  try
    eHorizontal.Text := Ini.ReadString( Sec, 'Desp_X' , '0');
    eVertical.Text   := Ini.ReadString( Sec, 'Desp_Y' , '0');
    eNumIni.Text     := Ini.ReadString( Sec, 'Num_Ini', '0');
    eNumFin.Text     := Ini.ReadString( Sec, 'Num_Fin', '0');
  finally
    Ini.Free;
  end;
end;

procedure TFormMain.lTituloDblClick(Sender: TObject);
begin
  if cbDocs_x_hoja.ItemIndex = 0 then
  begin
    frxReport.LoadFromFile( 'rep_CodBarras_Multas_6.fr3');
    frxReport.DesignReport;
  end;
end;


function TFormMain.GenerarDato( Numero, Ult_Num: Integer): String;
var
  Str_Num,
  digito : String;

  Procedure DC;
  var
    i  ,
    Tot,
    Num: Integer;
    cad: String;
  begin
    Tot := 0;
    For i := 0 to 7 do
      Tot := Tot + StrToInt( Str_Num[ i + 1]) * ( 8 - i);
    Digito := IntToStr( Tot mod 7);
  end;

begin
  Numero := Numero  * 10;
  if Numero <= ( Ult_Num * 10) then
  begin
    Str_Num := FormatFloat( '00000000', Numero);
    DC;
    Result := Str_Num + '-' + Digito + Str_Num + Digito + ' ';
  end
  else
    Result := DupeString( ' ', 20);
end;

procedure TFormMain.GenerarNumeros_6;
var
  Num_Ini,
  Num_Fin,
  Cant   ,
  Numero : LongInt;
  Linea  : String;
  Fich   : TextFile;
begin
  AssignFile( Fich, 'datos.csv');
  Rewrite( Fich);
  Num_Ini := StrToInt( eNumIni.Text);
  Num_Fin := StrToInt( eNumFin.Text);
  Num_ini := Num_Ini div 10;
  Num_Fin := Num_Fin div 10;
  Cant    := ( Num_Fin - Num_Ini) div 6;
  For Numero := Num_Ini to Num_Ini + Cant do
  begin
    Linea := GenerarDato( Numero                  , Num_Fin) +
             GenerarDato( Numero + ( Cant + 1) * 1, Num_Fin) +
             GenerarDato( Numero + ( Cant + 1) * 2, Num_Fin) +
             GenerarDato( Numero + ( Cant + 1) * 3, Num_Fin) +
             GenerarDato( Numero + ( Cant + 1) * 4, Num_Fin) +
             GenerarDato( Numero + ( Cant + 1) * 5, Num_Fin) ;
    Writeln( Fich, Linea);
  end;
  Flush( Fich);
  CloseFile( Fich);
end;

end.
